package com.fanwh.healthycampus2.fragment;


import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.afollestad.materialdialogs.GravityEnum;
import com.afollestad.materialdialogs.MaterialDialog;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.fanwh.healthycampus2.R;
import com.fanwh.healthycampus2.activity.AboutActivity;
import com.fanwh.healthycampus2.activity.ChangePswActivity;
import com.fanwh.healthycampus2.activity.LoginActivity;
import com.fanwh.healthycampus2.activity.PersonalInformationActivity;
import com.fanwh.healthycampus2.bean.HealthyInformation;
import com.fanwh.healthycampus2.bean.User;
import com.fanwh.healthycampus2.utils.ItemView;
import com.fanwh.healthycampus2.utils.ThemeManager;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobQueryResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.http.I;
import cn.bmob.v3.listener.SQLQueryListener;
import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.CropCircleTransformation;

/**
 * A simple {@link Fragment} subclass.
 */
public class PersonalFragment extends Fragment {

    private ImageView blurImageView;
    private ImageView avatarImageView;
    private TextView name;
    private TextView school;
    private ItemView exit;
    private ItemView about;
    private ItemView theme;
    private ItemView changepassword;
    private ItemView personal;



    public PersonalFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_personal, container, false);

        blurImageView = inflate.findViewById(R.id.iv_blur);
        avatarImageView = inflate.findViewById(R.id.iv_avatar);
        name = inflate.findViewById(R.id.user_name);
        school = inflate.findViewById(R.id.user_school);
        exit = inflate.findViewById(R.id.personal_exit);
        about = inflate.findViewById(R.id.personal_about);
        theme = inflate.findViewById(R.id.personal_theme);
        changepassword = inflate.findViewById(R.id.personal_change);
        personal = inflate.findViewById(R.id.personal_mine);

        initView();

        exit.setItemClickListener(new ItemView.itemClickListener() {
            @Override
            public void itemClick(String text) {
                new MaterialDialog.Builder(getActivity())
                        .title("提示")
                        .content("你确定要退出当前账号？")
                        .positiveText("确定")
                        .onPositive((dialog, which) ->{
                            Intent intent = new Intent(getActivity(), LoginActivity.class);
                            startActivity(intent);
                        })
                        .negativeText("取消")
                        .show();

            }
        });

        about.setItemClickListener(new ItemView.itemClickListener() {
            @Override
            public void itemClick(String text) {
                Intent intent = new Intent(getActivity(), AboutActivity.class);
                startActivity(intent);
            }
        });

        theme.setItemClickListener(new ItemView.itemClickListener() {
            @Override
            public void itemClick(String text) {
                String[] themes = ThemeManager.getInstance().getThemes();
                new MaterialDialog.Builder(getActivity())
                        .title("选择主题")
                        .titleGravity(GravityEnum.CENTER)
                        .items(themes)
                        .negativeText("取消")
                        .itemsCallback(((dialog, itemView, position, text1) -> {
                            ThemeManager.getInstance().setTheme(getActivity(), themes[position]);
                        }))
                        .show();
            }
        });

        changepassword.setItemClickListener(new ItemView.itemClickListener() {
            @Override
            public void itemClick(String text) {
                Intent intent = new Intent(getActivity(), ChangePswActivity.class);
                startActivity(intent);
            }
        });

        personal.setItemClickListener(new ItemView.itemClickListener() {
            @Override
            public void itemClick(String text) {
                Intent intent = new Intent(getActivity(), PersonalInformationActivity.class);
                startActivity(intent);
            }
        });


        return inflate;
    }

    private void initView() {

        Glide.with(getActivity()).load(R.drawable.head1)
                .bitmapTransform(new BlurTransformation(getActivity(), 25), new CenterCrop(getActivity()))
                .into(blurImageView);

        Glide.with(getActivity()).load(R.drawable.head1)
                .bitmapTransform(new CropCircleTransformation(getActivity()))
                .into(avatarImageView);

        User user = BmobUser.getCurrentUser(User.class);
        String username = (String) BmobUser.getObjectByKey("username");
        BmobQuery<HealthyInformation> query = new BmobQuery<HealthyInformation>();
        query.addWhereEqualTo("student",user);
        String sql = "SELECT * From HealthyInformation where studentID = '"+username+"'ORDER BY createdAt DESC LIMIT 1";
        query.doSQLQuery(sql, new SQLQueryListener<HealthyInformation>() {
            @Override
            public void done(BmobQueryResult<HealthyInformation> bmobQueryResult, BmobException e) {
                if(e == null){
                    List<HealthyInformation> list = bmobQueryResult.getResults();
                    //Toast.makeText(getActivity(),"查询成功：共"+list.size()+"条数据",Toast.LENGTH_SHORT).show();
                    for(HealthyInformation heathyInformation : list){
                        name.setText(heathyInformation.getName());
                        school.setText(heathyInformation.getSchool());
                    }

                }else {
                    System.out.println(e.getErrorCode());
                }
            }
        });
    }

}
